package cc.telecomdigital.tdstock;

import java.util.Vector;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

/**
 * Customer Dialog alert message
 * 
 * @author ouyangzhihui
 */
public class CustomizeDialog extends Dialog implements OnClickListener
{
	protected Button						okButton;
	protected TextView						caption;
	protected TextView						content;
	private String							_theContent;
	public static Vector<CustomizeDialog>	AllInstance		= new Vector<CustomizeDialog>();
	private final static int				ALLOW_MAX_SIZE	= 3;

	private boolean                          htmlStyle = true;
	
	private void init()
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.richdialog);
		okButton = (Button) findViewById(R.id.OkButton);
		okButton.setOnClickListener(this);

		caption = (TextView) findViewById(R.id.caption);
		content = (TextView) findViewById(R.id.content);
		while (AllInstance.size() > ALLOW_MAX_SIZE)
		{
			if (AllInstance.get(AllInstance.size() - 1) != null && AllInstance.get(AllInstance.size() - 1).isShowing())
			{
				AllInstance.get(AllInstance.size() - 1).dismiss();
			}
			AllInstance.remove(AllInstance.size() - 1);
		}
		AllInstance.add(this);
	}

	public CustomizeDialog(Context context)
	{
		super(context, R.style.dialog);
		init();
		//Disable the back
		setCancelable(false);
	}
	
	public CustomizeDialog(Context context, boolean isBack)
	{
		super(context, R.style.dialog);
		init();
		setCancelable(isBack);
	}

	public CustomizeDialog(Context context, String theCaption,
			String theContent)
	{
		super(context, R.style.dialog);
		init();
		caption.setText(theCaption);
		_theContent = theContent;
		showAsHtmlContent();
		//Disable the back
		setCancelable(false);
	}
	
	public CustomizeDialog(Context context, String theCaption,
			String theContent, boolean isBack)
	{
		super(context, R.style.dialog);
		init();
		caption.setText(theCaption);
		_theContent = theContent;
		showAsHtmlContent();
		setCancelable(isBack);
	}
	
	public CustomizeDialog(Context context, String theCaption,
			String theContent, String theButton)
	{
		super(context, R.style.dialog);
		init();
		//Disable the back
		setCancelable(false);
		caption.setText(theCaption);
		_theContent = theContent;
		showAsHtmlContent();
		okButton.setText(theButton);
	}
	
	public CustomizeDialog(Context context, String theCaption,
			String theContent, String theButton, boolean isBack)
	{
		super(context, R.style.dialog);
		init();
		setCancelable(isBack);
		caption.setText(theCaption);
		_theContent = theContent;
		showAsHtmlContent();
		setCancelable(isBack);
	}

	public CustomizeDialog(Context context, String theCaption,
			String theContent, String theButton, boolean iUseHTMLStyle, boolean isBack)
	{
		super(context, R.style.dialog);
		init();
		caption.setText(theCaption);
		_theContent = theContent;
		htmlStyle = iUseHTMLStyle;
		showAsHtmlContent();
		okButton.setText(theButton);
		setCancelable(isBack);
	}	
	
	public void setMsgContent(String msg)
	{
		_theContent = msg;
		showAsHtmlContent();
		content.postInvalidate();
	}

	@Override
	public void onClick(View v)
	{
		/** When OK Button is clicked, dismiss the dialog */
		if (v == okButton)
		{
			dismiss();
		}
	}

	
	public void setCaption(String iCapString)
	{
		caption.setText(iCapString);		
	}
	
	public void SetToUseHTMLStyle(boolean iEnable)
	{
		 htmlStyle = iEnable;
	}
	
	private void showAsHtmlContent()
	{
		if (_theContent == null)
		{
			return;
		}
		try
		{
			if (htmlStyle)
			{
				TDStockLog.d("content", _theContent);
				content.setText(Html.fromHtml(StringUtils.replaceAllCtLfWithBR(_theContent)));
			}
			else
				content.setText(_theContent);
		}
		catch (Exception e)
		{
			content.setText(_theContent);
		}
	}

	@Override
	public void show()
	{
		showAsHtmlContent();
		super.show();
	}

	public static void dismissAll()
	{
		while (AllInstance.size() > 0)
		{
			if (AllInstance.get(AllInstance.size() - 1) != null && AllInstance.get(AllInstance.size() - 1).isShowing())
			{
				AllInstance.get(AllInstance.size() - 1).dismiss();
			}
			AllInstance.remove(AllInstance.size() - 1);
		}
	}

//	@Override
//	public void onBackPressed()
//	{
//		if(isBack)
//			super.onBackPressed();
//	}
}
